package zpf.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import zpf.entity.User;

/**
 * @ClassName: MovieWithoutEurekaController
 * @Description: todo
 * @Autor: ZPF
 * @Date: 2021/1/13 11:22
 **/
@RestController
public class MovieWithoutEurekaController {

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private LoadBalancerClient loadBalancerClient;

    @GetMapping("/movie/{id}")
    public User findById(@PathVariable Long id) {
        return this.restTemplate.getForObject("http://provider-user/simple/" + id, User.class);
    }

    @GetMapping("/choose")
    public String testChoose() {
        ServiceInstance serviceInstance = this.loadBalancerClient.choose("provider-user");

        String serviceInfo = serviceInstance.getServiceId() + ":" + serviceInstance.getHost() + ":" + serviceInstance.getPort();

        System.out.println(serviceInfo);

        return serviceInfo;
    }

}
